package com.ly.mpayment.rest.system;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.debitlog.IDebitLogService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/debitlog/")
public class DebitLogRest extends AbsRest {

	@Autowired
	IDebitLogService debitService;

	@Autowired
	IUserService userService;

	@SuppressWarnings("static-access")
	@POST
	@Path("findPageDebitLog")
	public String findPageDebitLog(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize","starttime","endtime","useruuid","phoneNum");
		if(result == null){
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject userInfo = JsonUtils.converToJson(jwt.getSubject());
						if (userInfo != null) {
							JSONObject json = JSONObject.fromObject(data);
							Map<String, Object> parameters = MapUtil.getMap();
							User adminUser = userService.findByUserid(userInfo.getString(Config.tokenSignAttr));
							if (adminUser != null && UserUtil.checkRoleDown(adminUser)) {
									parameters.put(":useruuid", adminUser.getUseruuid());
							}

							if(!StringUtils.isBlank(json.getString("useruuid"))){
								parameters.put(":useruuid", json.getString("useruuid"));
							}
							if(!StringUtils.isBlank(json.getString("starttime"))){
								parameters.put("starttime", json.getString("starttime"));
							}
							if(!StringUtils.isBlank(json.getString("endtime"))){
								parameters.put("endtime", json.getString("endtime"));
							}
							if(!StringUtils.isBlank(json.getString("phoneNum"))){
								parameters.put(":phoneNum", json.getString("phoneNum"));
							}

							return ResultMessage.getSucceed( JsonUtils.excludes(debitService.findPageDebitLog(parameters, json.getInt("start"),
									json.getInt("pageSize")), new String[]{"downShopInfo","","downOrderRefunds","orderMatchDetails"
											,"upOrders","downOrderLogs","","discountAmount","callNum","discount","callStatus","busCode",
											"downreqnum","downendtimesec","downnotificationaddress","downrechargenum","rechargeamount","downendtime",
											"flowInformation","downShopProduct","userFlow","downreqid","operators","statusremark","createtime","","","","","","","","","","",""}));

						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return result;
	}

}
